<?php
require 'Controller.php ';
require 'UserModel.php';
class UserController extends Controller{
  public function __Construct(){
    parent::__Construct();
    $this->mod = 'users';
    $this->mod= new UserModel();
  }
  public function listusers(){
   
    // $model = new UserModel();
    $data = $this->mod->getUserList();
    $this->assign('users',$data);
    $this->display(); 
    //return $data;

  }
  public function userform(){
    $this->display();
  }
  public function adduser(){
    $data = $_POST;
    // $model = new UserModel();
    $result = $this->mod->addUserInfo($data);
    if($result){
      echo '添加成功 <a href="index.php?c=user&m=listusers">如果没有跳转，请点这里跳转</a>';
        header("refresh:3;url='index.php?c=user&m=listusers'");
    }
    else{
      echo "添加失败！<br/>";
        echo "<a href='index.php?c=user&m=userform'>返回</a>";
    }
  }
  public function edituser(){
    $id = $_GET['id'];
    // $model = new UserModel();
    $result = $this->mod->getUserbyID($id);
    $this->assign('user',$result);
    $this->display(); 
  }

  public function updateUser(){
    $data = $_POST;
    // $model = new UserModel();
    $result = $this->mod->updateUserInfo($data);
    if($result){
      echo '更新成功 <a href="index.php?c=user&m=listusers">如果没有跳转，请点这里跳转</a>';
        header("refresh:3;url='index.php?c=user&m=listusers'");
    }
    else{
      echo "更新失败！<br/>";
        echo "<a href='index.php?c=user&m=edituser&id=".$data['id']."'>返回</a>";
    }
  }
  public function delUser(){
    $id = $_GET['id'];
    // $model = new UserModel();
    $result = $this->mod->delUser($id);
    if($result){
      echo '删除成功 <a href="index.php?c=user&m=listusers">如果没有跳转，请点这里跳转</a>';
        header("refresh:3;url='index.php?c=user&m=listusers'");
    }
    else{
      echo "删除失败！<br/>";
        echo "<a href='index.php?c=user&m=listusers'>返回</a>";
    }
  }

}
?>